#标准库，用于创建图形用户界面（GUI），支持窗口、按钮、文本框等控件的创建和管理。
import tkinter as tk
from tkinter import PhotoImage
#第三方库，用于图像处理，支持图像打开、编辑、保存、格式转换等操作。Pillow 是 PIL 的现代分支，功能更强大且兼容性更好。
from PIL import Image, ImageTk
#标准库，提供与操作系统交互的功能，如文件路径操作、环境变量管理、目录创建与删除等。
import os
import json

#获取数据
from CodePython.Shared import *

#获取子界面
from CodePython.PageZero import *
from CodePython.PageStart import *
from CodePython.PageHome import *
from CodePython.PageRole import *
from CodePython.PageForge import *
from CodePython.PageFight import *
from CodePython.PageSet import *
from CodePython.PageZhandoushuang import *

class ZM(tk.Tk):
    def __init__(self):
        super().__init__()
        self.title("Kiana's Dreamland Adventure");''
        self.geometry("1200x720")
        self.frames = {}
        #数据初始化
        shareddata.player_initialization()
        shareddata.picture_initialization()
        shareddata.chapter_initialization()

        self.buffer_canvas = tk.Canvas(self, width = 1200, height = 720)
        self.buffer_canvas.grid(row = 0, column = 0, sticky = "nsew")
        self.buffer_image = Image.new("RGBA", (1200, 720), (255, 255, 255, 0))
        self.buffer_photo = ImageTk.PhotoImage(self.buffer_image)

        self.init_framesA()
        self.show_frame("PageZero")

    def init_framesA(self):
        for F in (PageZero, PageStart):
            frame = F(self)
            self.frames[F.__name__] = frame
            frame.grid(row=0, column=0, sticky="nsew")

    #分开加载保证在加载这些界面的canvas时player数据已经经由PageStart的初始化操作更新为最新内容
    def init_framesB(self):
        for F in (PageHome, PageRole, PageForge, PageFight, PageSet):
            frame = F(self)
            self.frames[F.__name__] = frame
            frame.grid(row=0, column=0, sticky="nsew")

    def init_framesC(self):
        for F in (PageZhandoushuang, ):
            frame = F(self)
            self.frames[F.__name__] = frame
            frame.grid(row=0, column=0, sticky="nsew")

    def show_frame(self, page_name):
        frame = self.frames[page_name]
        frame.tkraise()  
        self.update_buffer()

    def update_buffer(self):
        self.buffer_image.paste((255, 255, 255, 0), [0, 0, 1200, 720])
        for frame in self.frames.values():
            frame.draw_to_buffer(self.buffer_image,self)
        self.buffer_photo = ImageTk.PhotoImage(self.buffer_image)
        self.buffer_canvas.create_image(0, 0, image = self.buffer_photo, anchor = tk.NW)

    def get_image(self,image_path,width,height):
        return (Image.open(shareddata.get_ROOT_DIRECTORY() + image_path).resize((width, height))).convert("RGBA"), ImageTk.PhotoImage((Image.open(shareddata.get_ROOT_DIRECTORY() + image_path).resize((width, height))).convert("RGBA"))
    
    def Initialization(self):
        shareddata.set_player_path((shareddata.get_player_path().replace("*",shareddata.get_neckname())))
        if(Do_file_exit()):
            Do_file_read()
        else:
            player = shareddata.get_player()
            Do_file_write()
    

def Do_file_exit():
    if(os.path.exists(shareddata.get_player_path())):
        return True
    return False

def Do_file_read():
     with open(shareddata.get_player_path(), 'r', encoding = 'utf-8') as f:
        player = json.load(f)
        shareddata.set_player(player)

def Do_file_write():
    with open(shareddata.get_player_path(), 'w', encoding = 'utf-8') as f:
        json.dump(shareddata.get_player(), f, indent = 4)


if __name__ == "__main__":
    app = ZM()
    app.mainloop()